using System;
using System.Collections.Generic;
using System.Text;
using LiveStock.RssObjects;
using LiveStock.dataAccess;
using LiveStock.Evaluation;

namespace LiveStock.Recommendation
{
    class RecommendationManager
    {
        public static void InsertRecommendation(Stock theStock, int userID)
        {
            //Create a New companies row for insertion into the database.
            //set its stock values = to the searchstock values, and user values to curUser
            Companies.CompaniesRow theCompaniesRow = new Companies.CompaniesDataTable().NewCompaniesRow();
            theCompaniesRow.UserID = userID;
            theCompaniesRow.CompanyName = theStock.CompanyName;
            theCompaniesRow.Symbol = theStock.StockSymbol;
            theCompaniesRow.Exchange = theStock.Exchange;
            int companyID = CompaniesData.CompanyInsert(theCompaniesRow);

            ProcessRecommendation(companyID, theStock.StockSymbol, userID, (decimal)theStock.LastTrade);
        }

        public static void UpdateRecommendation(int companyID, string stockSymbol, int userID)
        {
            ProcessRecommendation(companyID, stockSymbol, userID, 0);
        }

        public static void ProcessRecommendation(int companyID, string stockSymbol, int userID, decimal stockPrice)
        {
            //Insert initial recommendation row
            int recommendationID = InsertRecommendationRow(companyID, stockPrice);

            //Get the evalutions            
            EvaluationManager theEvaluationManager = new EvaluationManager();
            List<IEvaluation> evaluationList = theEvaluationManager.GetEvaluationList();
            int evalutionListRating = 0;
            foreach (IEvaluation evaluation in evaluationList)
            {
                evalutionListRating += ProcessNewsItemList(evaluation.getNewsItems(stockSymbol), recommendationID, userID);
            }

            //Set recommendationRating to average of evaluationList
            int recommendationRating = evalutionListRating / evaluationList.Count;
            int ratingTypeID = RatingTypesData.RatingTypeIDGetByRating(recommendationRating);
            //Check to make sure return a valid ratingTypeID
            if (ratingTypeID != 0)
            {
                //Update recommendation on database.
                Recommendations.RecommendationsDataTable rdt = RecommendationsData.RecommendationGetByID(recommendationID);
                if (rdt.Rows.Count > 0)
                {
                    rdt[0].RatingTypeID = ratingTypeID;
                    rdt[0].RecommendationDate = DateTime.Now;
                    rdt[0].Rating = recommendationRating;
                    RecommendationsData.RecommendationUpdate(rdt[0]);
                }
            }
        }

        private static int ProcessNewsItemList(List<NewsItem> newsItemList, int recommendationID, int userID)
        {
            int newsItemListRating = 0;
            foreach (NewsItem theNewsItem in newsItemList)
            {
                //Get news items rating and add it to newsItemListRating
                string newsArticle = theNewsItem.Title + " " + theNewsItem.Description;
                //string newsArticle = HttpTextGrabber.GrabTextFromUrl(theNewsItem.URL);
                int evaluationRating = EvaluationRating.GetRating(userID, newsArticle, theNewsItem.PublishDate);
                newsItemListRating += evaluationRating;

                //Save evaluation to database.
                Evaluations.EvaluationsRow er = new Evaluations.EvaluationsDataTable().NewEvaluationsRow();
                er.EvaluationSourceID = theNewsItem.EvaluationSourceID;
                er.RecommendationID = recommendationID;
                er.RatingTypeID = RatingTypesData.RatingTypeIDGetByRating(evaluationRating);
                er.Description = theNewsItem.Description;
                er.URL = theNewsItem.URL;
                er.EvaluationDate = DateTime.Now;
                er.Rating = evaluationRating;
                er.Title = theNewsItem.Title;
                EvaluationsData.EvaluationInsert(er);
            }
            //return average newsItemListRating
            if (newsItemList.Count > 0)
            {
                return newsItemListRating / newsItemList.Count;
            }
            else
                return newsItemListRating;
        }

        private static int InsertRecommendationRow(int companyID, decimal price)
        {
            int recommendationID;
            //Insert initial recommendation row
            Recommendations.RecommendationsRow theRecommendationsRow = new Recommendations.RecommendationsDataTable().NewRecommendationsRow();
            theRecommendationsRow.CompanyID = companyID;
            //Default data
            theRecommendationsRow.RatingTypeID = RatingTypesData.RatingTypeIDGetByRating(0);
            theRecommendationsRow.RecommendationDate = DateTime.Now;
            theRecommendationsRow.Rating = 0;
            theRecommendationsRow.Price = price;
            recommendationID = RecommendationsData.RecommendationInsert(theRecommendationsRow);
            return recommendationID;
        }

    }
}
